package com.zw.yzk.kabu.presenter;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.QueryOrderStateEntity;
import com.zw.yzk.kabu.data.network.entity.SubmitOrderEntity;
import com.zw.yzk.kabu.data.network.task.QueryOrderStateTask;
import com.zw.yzk.kabu.data.network.task.SubmitOrderTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.model.SubmitGoods;
import com.zw.yzk.kabu.model.SubmitOrder;
import com.zw.yzk.kabu.utils.WechatPayInfo;
import com.zw.yzk.kabu.view.activity.MyGoodsOrderActivity;
import com.zw.yzk.kabu.view.view.ConfirmGoodsOrderView;
import com.zw.yzk.kabu.wxapi.WechatManager;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/12.
 */

public class ConfirmGoodsOrderPresenter implements Presenter {

    private UseCase submitOrder;
    private UseCase checkOrderState;
    private AppCompatActivity activity;
    private ConfirmGoodsOrderView confirmGoodsOrderView;
    private String tradeId;

    @Inject
    public ConfirmGoodsOrderPresenter(@Named("submitOrderUseCase") UseCase submitOrder,
                                      @Named("queryOrderUseCase") UseCase checkOrderState,
                                      AppCompatActivity activity) {
        this.submitOrder = submitOrder;
        this.checkOrderState = checkOrderState;
        this.activity = activity;
    }

    public void setView(ConfirmGoodsOrderView view) {
        this.confirmGoodsOrderView = view;
    }

    public void setSubmitOrder(List<SubmitOrder> orderList) {
        SubmitOrderTask task = new SubmitOrderTask();
        task.details = new ArrayList<>();
        List<Integer> shoppingCartIdList = new ArrayList<>();
        for (SubmitOrder order : orderList) {
            SubmitOrderTask.OrderDetails orderDetails = new SubmitOrderTask.OrderDetails();
            orderDetails.shopId = order.getShopId();
            orderDetails.receiverInfoId = order.getReceiverInfoId();
            List<SubmitOrderTask.Goods> goodsList = new ArrayList<>();
            for (SubmitGoods goods : order.getGoodsList()) {
                SubmitOrderTask.Goods g = new SubmitOrderTask.Goods();
                g.goodsId = goods.getGoodsId();
                g.count = goods.getCount();
                g.skuId = goods.getSkuId();
                shoppingCartIdList.add(goods.getShoppingCardId());
                goodsList.add(g);
            }
            orderDetails.goodsList = goodsList;

            task.details.add(orderDetails);
        }
        task.shoppingcartId = shoppingCartIdList.isEmpty() ? null : shoppingCartIdList;
        submitOrder.execute(new ConfirmGoodsOrderSubscriber(activity), task);
    }

    public void checkOrderState() {
        if (TextUtils.isEmpty(tradeId)) {
            return;
        }
        checkOrderState.execute(new QueryOrderStateSubscriber(activity), new QueryOrderStateTask(tradeId));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        submitOrder.unSubscribe();
        checkOrderState.unSubscribe();
    }

    private class QueryOrderStateSubscriber extends BaseSubscriber<QueryOrderStateEntity> {

        public QueryOrderStateSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public void onNext(QueryOrderStateEntity entity) {
            super.onNext(entity);
            if (entity.result.result == 1) {
                ToastManager.getInstance().showToast(activity, activity.getResources().getString(R.string.paid_success));
//                confirmGoodsOrderView.queryOrderStateSuccess();
            }
            tradeId = "";
            activity.startActivity(new Intent(activity, MyGoodsOrderActivity.class));
            activity.finish();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            tradeId = "";
            activity.startActivity(new Intent(activity, MyGoodsOrderActivity.class));
            activity.finish();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.check_order_state_by_yourself);
        }
    }

    private class ConfirmGoodsOrderSubscriber extends BaseSubscriber<SubmitOrderEntity> {

        public ConfirmGoodsOrderSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(SubmitOrderEntity entity) {
            super.onNext(entity);
            //设置支付信息
            tradeId = entity.result.details.tradeId;
            //调起微信支付
            WechatManager.payByWeChat(activity, entity.result.wechatPay.appId, entity.result.wechatPay.partnerId, entity.result.wechatPay.prepayId,
                    entity.result.wechatPay.packageName, entity.result.wechatPay.nonceStr, entity.result.wechatPay.timeStamp, entity.result.wechatPay.sign);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.submit_order_failed);
        }
    }
}
